#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <stddef.h>

#include <errno.h>
#include <string.h>

int main(int argc,char *argv[])
{
    int sockfd = 0;
    struct sockaddr_un addr;
    int len, counter = 0;
    char send_buf[20] = "";


    if (argc != 2) {
        printf("usage: %s <domain service path>\n", argv[0]);
        return 1;
    }

    memset(&addr, 0, sizeof(addr));
    addr.sun_family = AF_UNIX;
    strcpy(addr.sun_path, argv[1]);
    len = offsetof(struct sockaddr_un, sun_path) + strlen(argv[1]);
    // len = sizeof(addr.sun_family) + strlen(addr.sun_path); // Same as above


    sockfd = socket(AF_UNIX,SOCK_DGRAM,0);
    if(sockfd < 0) {
        printf("socket error %d: %s\n", errno, strerror(errno));
        exit(-1);
    }

    while(1) {
        snprintf(send_buf,sizeof(send_buf),"Counter is %d", ++counter);
        if (sendto(sockfd,send_buf,strlen(send_buf), 0, (struct sockaddr*)&addr, len) < 0)
            printf("sendto error %d: %s\n", errno, strerror(errno));
        else
            printf("Send: %s\n",send_buf);
        sleep(1);
    }
    return 0;
}
